package com.snowy.hidden.biz.controller;


import com.snowy.demo.log.annotation.SysLog;
import com.snowy.framework.common.base.controller.BaseController;
import com.snowy.framework.common.base.vo.LoginUser;
import com.snowy.framework.common.resp.BaseResp;
import com.snowy.framework.common.resp.BaseRespUtil;
import com.snowy.framework.exception.UABPException;
import com.snowy.hidden.biz.domain.model.QuestionBank;
import com.snowy.hidden.biz.domain.model.QuestionOptions;
import com.snowy.hidden.biz.domain.req.SubmitUserAnswersReq;
import com.snowy.hidden.biz.domain.resp.QuestionBankListResp;
import com.snowy.hidden.biz.domain.resp.SubmitUserAnswersResp;
import com.snowy.hidden.biz.service.QuestionBankService;
import com.snowy.hidden.constant.AuthConstant;
import com.snowy.hidden.paging.PageQueryModel;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 问答题库表(QuestionBank)表控制层
 *
 * @author makejava
 * @since 2025-03-08 19:53:41
 */
@RestController
@RequestMapping("questionBank")
public class QuestionBankController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private QuestionBankService questionBankService;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
	@SysLog("QuestionBank查询单条数据")
    @GetMapping("/getById/{id}")
    public BaseResp selectOne(@PathVariable("id") Long id) {
		try {
            QuestionBank tempObject = questionBankService.getById(id);
            return BaseRespUtil.Success(tempObject);
        } catch (UABPException e) {
            return BaseRespUtil.Error(e);
        }
    }

    /**
     * 新增数据
     *
     * @param questionBank 实体对象
     * @return 新增结果
     */
	@SysLog("QuestionBank新增数据")
    @PostMapping("/save")
    public BaseResp insert(@RequestBody QuestionBank questionBank) {
		try {
            questionBankService.save(questionBank);
            return BaseRespUtil.Success();
        } catch (UABPException e) {
            return BaseRespUtil.Error(e);
        }
	
    }

    /**
     * 修改数据
     *
     * @param questionBank 实体对象
     * @return 修改结果
     */
	@SysLog("QuestionBank修改数据")
    @PostMapping("/updateById")
    public BaseResp update(@RequestBody QuestionBank questionBank) {
		try {
            questionBankService.updateById(questionBank);
            return BaseRespUtil.Success();
        } catch (UABPException e) {
            return BaseRespUtil.Error(e);
        }
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
	@SysLog("QuestionBank删除数据")
    @PostMapping("/removeByIds")
    public BaseResp delete(@RequestParam("idList") List<Long> idList) {
		try {
            questionBankService.removeByIds(idList);
            return BaseRespUtil.Success();
        } catch (UABPException e) {
            return BaseRespUtil.Error(e);
        }
    }
	 /**
     * 分页查询数据
     *
     * @param pageQueryModel 分页模板
     * @return 删除结果
     */
	@SysLog("QuestionBank分页查询数据")
    @PostMapping("/queryPage")
    public BaseResp queryPage(@RequestBody PageQueryModel pageQueryModel) {
		try {
            BaseResp baseResp=questionBankService.queryPage(pageQueryModel);
            return BaseRespUtil.Success(baseResp);
        } catch (UABPException e) {
            return BaseRespUtil.Error(e);
        }
    }


    /**
     * 依据登陆人员获取试题列表
     */
    @SysLog("QuestionBank")
    @PostMapping("/queryList")
    public BaseResp queryList() {
        try {
            LoginUser userRedisInfo = AuthConstant.userLocal.get();
            List<QuestionBankListResp> questionBankListRespList=questionBankService.queryList(userRedisInfo);
            return BaseRespUtil.Success(questionBankListRespList);
        } catch (UABPException e) {
            return BaseRespUtil.Error(e);
        }
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @SysLog("获取答案列表")
    @GetMapping("/getOptions/{questionId}")
    public BaseResp getOptions(@PathVariable("questionId") String questionId) {
        try {
            List<QuestionOptions> tempObject = questionBankService.getOptions(questionId);
            return BaseRespUtil.Success(tempObject);
        } catch (UABPException e) {
            return BaseRespUtil.Error(e);
        }
    }

    /**
     * 提交做题答案 ，返回当前此提的分数和用户总分，如果错误则不记录到答题库里面
     * @return
     */
    @SysLog("QuestionBank")
    @PostMapping("/submitquery")
    public BaseResp submitquery(@RequestBody SubmitUserAnswersReq submitUserAnswersReq) {
        try {
            LoginUser userRedisInfo = AuthConstant.userLocal.get();
            SubmitUserAnswersResp submitUserAnswersResp=questionBankService.submitquery(submitUserAnswersReq,userRedisInfo);
            return BaseRespUtil.Success(submitUserAnswersResp);
        } catch (UABPException e) {
            return BaseRespUtil.Error(e);
        }
    }


}
